package ltd.newbee.mall.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class JdbcController {
    //已经自动配置，可以直接通过@Autowired注入进来
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/insert")
    public String insert(String type, String name){
        if(StringUtils.isEmpty(type) || StringUtils.isEmpty(name)){
            return "参数异常";
        }
        jdbcTemplate.execute("insert into jdbc_test(ds_type, ds_name) " +
                "value (\"" + type + "\", \"" + name + "\")");
        return "SQL执行完毕";
    }

    //删除一条记录
    @GetMapping("/delete")
    public String delete(int id){
        if(id < 0){
            return "参数异常";
        }
        List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from jdbc_test where ds_id=\"" + id + "\"");
        if(CollectionUtils.isEmpty(result)) {
            return "不存在该记录，删除失败";
        }
        jdbcTemplate.execute("delete from jdbc_test where ds_id=\"" + id + "\"");
        return "SQL执行完毕";
    }

    //修改一条记录
    @GetMapping("/update")
    public String update(int id, String type, String name) {
        if(id < 0 || StringUtils.isEmpty(type) || StringUtils.isEmpty(name)) {
            return "参数异常";
        }
        List<Map<String, Object>> result = jdbcTemplate.queryForList("select * from jdbc_test where ds_id =\"" + id + "\"");
        if (CollectionUtils.isEmpty(result)){
            return "不存在该记录，无法修改";
        }
        jdbcTemplate.execute("update jdbc_test set ds_type=\"" + type + "\", ds_name=\"" + name + "\" where ds_id=\"" + id + "\"");
        return "SQL 执行完毕";
    }

    //查询所有记录
    @GetMapping("/queryAll")
    public List<Map<String, Object>> queryAll(){
        List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from jdbc_test");
        return list;
    }

    @GetMapping("/testTest")
    public List<Map<String, Object>> testTest(){
        List<Map<String, Object>> list = new ArrayList<>();
        return list;
    }

    @GetMapping("/testTest2")
    public Map<String, Map<String, List<String>>> testTest2(){
        Map<String, Map<String, List<String>>> list = new HashMap<>();
        return list;
    }


}
